package com.cmower.mkyong.elasticsearch;

import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;
import java.net.InetAddress;

public class ElasticsearchTest {
    public static void main(String[] args) throws IOException {

        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http")));

        IndexRequest indexRequest = new IndexRequest("writer")
                .id("1")
                .source("name", "沉默王二",
                        "age", 18,
                        "memo", "一枚有趣的程序员");
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);

        GetRequest getRequest = new GetRequest(
                "writer",
                "1");

        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
        String sourceAsString = getResponse.getSourceAsString();

        System.out.println(sourceAsString);
        client.close();
    }
}
